import dayjs from "dayjs"
import Notify from "@vant/weapp/notify/notify"
import {patch} from "@utils/http"
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import {planStore} from "@store/plans"
import {getPlanMixin} from "@mixin/plan"
import { tipMixin } from "@mixin/tip"
Page({
  mixins:[getPlanMixin,tipMixin],
  /**
   * 页面的初始数据
   */
  data: {
    nowDate:dayjs().format("YYYY-MM-DD"),
    nowTime:dayjs().format("HH:mm:ss"),
    modes:["从不","每天","每周","每月"],
    tags:["学习","娱乐","交友"],
    form:{
      id:0,
      name:"",
      mode:"学习",
      date:"",
      time:"",
      repetition:"从不",
      remarks:"",
      isTop:false,
      isAllDay:false,
      isMind:"",
      start:"",
      cron:"",
      repetitionIndex:0,
    },
    loading:false
  },
  saveFormData:function(e:any){
    this.setData({
      form:{
        ...this.data.form,
        [e.currentTarget.dataset.formkey]:e.detail.value
      }
    })
  },
  changeMode:function(e:any){
    const{value}=e.currentTarget.dataset
    this.setData({
      form:{
        ...this.data.form,
        mode:value
      }
    })
  },
  changeRepetition:function(e:any){
    console.log(e.detail.value)
    this.setData({
      form:{
        ...this.data.form,
        repetition:this.data.modes[e.detail.value],
        repetitionIndex:e.detail.value,
      }
    })
  },
  verify(){
    const{name,date,time}=this.data.form
    console.log(name,date,time)
    if(!name.trim()||!date.trim()||!time.trim()){
      this.tip("请填写完整信息")
      return false
    }
    return true
  },
  submit:async function(){
    const{form}=this.data
    console.log(form)
    if(!this.verify())return
    this.setData({
      loading:true
    })
    form.start=dayjs(`${form.date} ${form.time}`).format("YYYY-MM-DD HH:mm:ss")
    const start=dayjs(form.start)
    switch(form.repetition){
        case "从不":
         form.cron=start.format("ss mm HH DD MM ? YYYY")
          break
        case "每天":
         form.cron= start.format("ss mm HH * * ?")
          break
        case "每周":
         form.cron= start.format(`ss mm HH ? * ${start.get("day")}`)
          break
        case "每月":
          form.cron=start.format("ss mm HH DD * ?")
          break
    }
    
    await patch("/plan",null,{
      id:form.id,
      name:form.name,
      mode:form.mode,
      isAllDay:form.isAllDay,
      remarks:form.remarks,
      isMind:form.isMind,
      cron:form.cron,
      isTop:form.isTop
    })
    this.setData({
      loading:false
    })
    this.tip("编辑成功","success")
    this.getPlan()
    wx.navigateBack({
        delta:2
    })
  },
  tip:function(message:string,type?:string){},
  /**
   * 生命周期函数--监听页面加载
   */
  storeBindings:null,
  onLoad() {
    this.storeBindings=createStoreBindings(this,{
      store:planStore,
      fields:{
        form:"planDetail"
      },
      actions:["setPlanList","setWeekStatusList"]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})